<?php

namespace App\Http\Admin\HelpsTraits;

trait DataTransform
{
    /**
     * @param $data
     * @return array
     */
    public function permDataTransform($data): array
    {
        $permRoutes = [];
        foreach ($data as $k => $v) {
            $newPermRoutes['name'] = str_replace(".", "_", $v['name']);
            $newPermRoutes['path'] = $v['router'];
            $newPermRoutes['component'] = match ($v['type']) {
                0 => $v['parent_id'] ? 'multi' : 'basic',
                1 => 'self',
//                1 => isset($v['children'])&&$v['children'] ? 'multi' : 'self',
            };
            $metes['title'] = $v['title'];
            $metes['icon'] = $v['icon'];
            $metes['order'] = $v['order_num'];
            if ($v['type'] == 1) {
                $metes['keepAlive'] = $v['keep_alive'] == 1;
                $metes['hide'] = $v['is_show'] == 0;
                $metes['requiresAuth'] = true;
            }
            $permRoutes[$k] = $newPermRoutes;
            $permRoutes[$k]['meta'] = $metes;
            if (isset($v['children']) && !empty($v['children']))
                $permRoutes[$k]['children'] = $this->permDataTransform($v['children']);
        }
        return $permRoutes;
    }

}
